package suncertify.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import suncertify.application.Log;
import suncertify.application.Strings;

/**
 * This class contains only static methods that provide user interface functionality.
 */
public class DesktopGui {

    /**
     * This function shows the input parameters in a java {@link JDialog} that consists of one OK button. the dialog
     * shows the standard JVM error icon as defined in {@link JOptionPane#ERROR_MESSAGE}.
     * 
     * @param title
     *            The string that is shown in the title bar of the dialog.
     * @param message
     *            The string message that is shown in the dialog.
     */
    public static void showErrorMessage(String title, String message) {

	showMessage(title, message, JOptionPane.ERROR_MESSAGE);
    }

    /**
     * This function shows the input parameters in a java {@link JDialog} that consists of one OK button. the dialog
     * shows the standard JVM information icon as defined in {@link JOptionPane#INFORMATION_MESSAGE}.
     * 
     * The string that is shown in the title bar of the dialog.
     * 
     * @param title
     *            The string that is shown in the title bar of the dialog.
     * @param message
     *            The string message that is shown in the dialog.
     */
    public static void showInfoMessage(String title, String message) {

	showMessage(title, message, JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * Shows the standard java {@link JOptionPane} dialog with the OK button, this function is used to show information
     * and error messages.
     * 
     * @param title
     *            The string that is shown in the title bar of the dialog.
     * @param message
     *            The string message that is shown in the dialog.
     * @param messageType
     *            One of {@link JOptionPane} message types e.g. {@link JOptionPane#ERROR_MESSAGE} or
     *            {@link JOptionPane#INFORMATION_MESSAGE}.
     */
    private static void showMessage(String title, String message, int messageType) {

	JOptionPane alert = new JOptionPane(message, messageType, JOptionPane.DEFAULT_OPTION);
	JDialog dialog = alert.createDialog(null, title);

	dialog.setLocation(getCenterPoint(dialog.getWidth(), dialog.getHeight()));
	dialog.setVisible(true);
    }

    /**
     * Calls {@link #showErrorMessage(String, String)}, the first parameter is {@link Strings#CAP_ERROR} and the second
     * parameter is the exception's message retrieved by {@link Exception#getMessage()}.
     * 
     * @param ex
     *            The exception whose message is to be displayed.
     */
    public static void showPerformedActionErrorMessage(Exception ex) {

	String message = Strings.getMessage(Strings.MSG_ACTION_LISTENER_GENERAL_EXCEPTION, ex.getMessage() != null ? ex
		.getMessage() : "");

	showErrorMessage(Strings.CAP_ERROR, message);
    }

    /**
     * Sets the UI look and feel of the JVM to the look and feel supported by the host. The look and feel is set as
     * follows:
     * 
     * <pre>
     * UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     * </pre>
     */
    public static void setUILookAndFeel() {
	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (UnsupportedLookAndFeelException uex) {
	    Log
		    .logInfo("UnsupportedLookAndFeelException is thrown by: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())");
	} catch (ClassNotFoundException cex) {
	    Log
		    .logInfo("ClassNotFoundException is thrown by: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())");
	} catch (InstantiationException iex) {
	    Log
		    .logInfo("InstantiationException is thrown by: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())");
	} catch (IllegalAccessException iaex) {
	    Log
		    .logInfo("IllegalAccessException is thrown by: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())");
	}
    }

    /**
     * Calculates the position that represents the center of a component with the given width and height. The position
     * is calculated as follows:
     * 
     * <pre>
     * Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
     * int x = (int) ((d.getWidth() - componenetWidth) / 2);
     * int y = (int) ((d.getHeight() - componenetHeight) / 2);
     * 
     * return new Point(x, y);
     * </pre>
     * 
     * @param componenetWidth
     *            The width of the component to be shown in the center of the screen.
     * @param componenetHeight
     *            The height of the component to be shown in the center of the screen.
     * @return The position that places the component in the center.
     */
    public static Point getCenterPoint(int componenetWidth, int componenetHeight) {

	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	int x = (int) ((d.getWidth() - componenetWidth) / 2);
	int y = (int) ((d.getHeight() - componenetHeight) / 2);

	return new Point(x, y);
    }
}
